<?php
namespace App\Api\Controllers\Lakala;

use App\Api\Controllers\lakala\BaseController as base;
use Illuminate\Support\Facades\Log;
use function EasyWeChat\Kernel\Support\get_client_ip;
class PayController extends BaseController
{
    public function __construct()
    {

    }


    public function scan_pay($data, $config)
    {
        try {
            $params = [
                'merchantNo' => $data['mch_id'],
                'appCode' => $config->app_code,
                'outTradeNo' => $data['out_trade_no'],
                'totalAmount' => $data['total_amount'],
                'goodSubject' => '扫码支付',
                'authCode' => $data['code'],
                'spbillCreateIp' => get_client_ip()
            ];
            if (isset($data['body']) && !empty($data['body'])) {
                $params['body'] = $data['body'];
            }
            $base = base::getInstance();
            $ret  = $base->params($params, $data['mch_id'], $data['key'])->build($base::SCAN_PAY, 'scan_pay');

            if (isset($ret['body']['rspStatus']) && !empty($ret['body']['rspStatus'])) {
                //PayResult 0成功，非0失败
                if ($ret['body']['rspStatus'] == "S") { //支付成功
                    if ($data['ways_source'] == 'weixin' && isset($ret['body']['wxUniqueInfo'])) {
                        \App\Common\PaySuccessAction::addCoupon($data['out_trade_no'], $ret['body']['wxUniqueInfo'], 'pay');
                    }
                    if ($data['ways_source'] == 'alipay' && isset($ret['body']['aliUniqueInfo'])) {
                        \App\Common\PaySuccessAction::addCoupon($data['out_trade_no'], $ret['body']['aliUniqueInfo'], 'pay');
                    }
                    if ($data['ways_source'] == 'unionpay' && isset($ret['body']['ysfUniqueInfo'])) {
                        \App\Common\PaySuccessAction::addCoupon($data['out_trade_no'], $ret['body']['ysfUniqueInfo'], 'pay');
                    }

                    return [
                        'status' => 1,
                        'message' => '支付成功',
                        'data' => $ret['body']
                    ];
                } else if ($ret['body']['rspStatus'] == 'P') {
                    return [
                        'status' => 2,
                        'message' => '支付中',
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['body']['rspMsg'])) {
                        $message = $ret['body']['rspMsg'];
                    }
                    return [
                        'status' => 3,
                        'message' => $message,
                    ];
                }
            } else {
                return [
                    'status' => 3,
                    'message' => '支付异常',
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 3,
                'message' => $ex->getMessage().' | '.$ex->getLine(),
            ];
        }
    }


    //查询订单 0-系统错误 1-成功 2-正在支付 3-失败 4.已经退款
    public function order_query($data, $config)
    {
        try {
            $params = [
                'outTradeNo' => $data['out_trade_no']
            ];
            $base = base::getInstance();
            $ret  = $base->params($params, $data['mch_id'], $data['key'])->build($base::QUERY_ORDER, 'order_query');

            if (isset($ret['body']['rspStatus']) && !empty($ret['body']['rspStatus'])) {
                //PayResult 0成功，非0失败
                if ($ret['body']['trxStatus'] == "success") { //支付成功
                    if ($data['ways_source'] == 'weixin' && isset($ret['body']['wxUniqueInfo'])) {
                        \App\Common\PaySuccessAction::addCoupon($data['out_trade_no'], $ret['body']['wxUniqueInfo'], 'query');
                    }
                    if ($data['ways_source'] == 'alipay' && isset($ret['body']['aliUniqueInfo'])) {
                        \App\Common\PaySuccessAction::addCoupon($data['out_trade_no'], $ret['body']['aliUniqueInfo'], 'query');
                    }
                    if ($data['ways_source'] == 'unionpay' && isset($ret['body']['ysfUniqueInfo'])) {
                        \App\Common\PaySuccessAction::addCoupon($data['out_trade_no'], $ret['body']['ysfUniqueInfo'], 'query');
                    }
                    return [
                        'status' => 1,
                        'message' => '支付成功',
                        'data' => $ret['body']
                    ];
                } else if ($ret['body']['trxStatus'] == 'paying') {
                    return [
                        'status' => 2,
                        'message' => '支付中',
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['body']['rspMsg'])) {
                        $message = $ret['body']['rspMsg'];
                    }
                    return [
                        'status' => 3,
                        'message' => $message,
                    ];
                }
            } else {
                return [
                    'status' => 3,
                    'message' => '支付异常',
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => '0',
                'message' => $ex->getMessage().' | '.$ex->getLine(),
            ];
        }
    }


    //退款 0-系统错误 1-成功
    public function refund($data, $config)
    {
        try {
            $params = [
                'merchantNo' => $data['mch_id'],
                'outTradeNo' => $data['out_trade_no'],
                'refundFee' => $data['refund_amount'],
                'outRefundNo' => $data['refund_no'],
                'spbillCreateIp' => get_client_ip()
            ];
            $base = base::getInstance();
            $ret  = $base->params($params, $data['mch_id'], $data['key'])->build($base::REFUND, 'refund');

            if (isset($ret['body']['rspStatus']) && !empty($ret['body']['rspStatus'])) {

                if ($ret['body']['rspStatus'] == 'S') {
                    \App\Common\PaySuccessAction::addCoupon($data['out_trade_no'], '', 'refund');
                    return [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => [
                            'refund_count' => 1,
                            'out_trade_no'  => $data['out_trade_no'],
                        ]
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['body']['rspMsg'])) {
                        $message = $ret['body']['rspMsg'];
                    }
                    return [
                        'status' => 2,
                        'message' => $message
                    ];
                }
            } else {
                $message = '失败了';
                return [
                    'status' => 2,
                    'message' => $message
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 0,
                'message' => $ex->getMessage().' | '.$ex->getLine()
            ];
        }
    }


    //退款查询 0-系统错误 1-成功 2-正在退款 3-失败
    public function refund_query($data)
    {
        try {


        } catch (\Exception $ex) {
            return [
                'status' => '0',
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
            ];
        }
    }

    public function qr_submit($data, $config)
    {
        try {
            $totalAmount = number_format($data['total_amount'], 2, '.', '');
            $params = [
                'merchantNo' => $data['mch_id'],
                'appCode' => $config->app_code,
                'outTradeNo' => $data['out_trade_no'],
                'totalAmount' => $totalAmount,
                'goodSubject' => '条码支付',
                'notifyUrl' => url('/api/lakala/pay_notify_url'),
            ];

            $base = base::getInstance();
            $ret  = $base->params($params, $data['mch_id'], $data['key'])->build($base::QR_SUBMIT, 'qr_submit');

            if (isset($ret['body']['rspStatus']) && !empty($ret['body']['rspStatus'])) {
                if ($ret['body']['rspStatus'] == 'S') {
                    return [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => [
                            'codeUrl' => $ret['body']['qrUrl']
                        ]
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['body']['rspMsg'])) {
                        $message = $ret['body']['rspMsg'];
                    }
                    return [
                        'status' => 2,
                        'message' => $message
                    ];
                }
            }
            return [
                'status' => 2,
                'message' => '接口异常'
            ];
        } catch (\Exception $ex) {
            return [
                'status' => 3,
                'message' => $ex->getMessage().' | '. $ex->getLine(),
            ];
        }
    }





}
